package src.controller;


import java.rmi.RemoteException;

import java.util.Set;


import javax.swing.DefaultListModel;

import javax.swing.ListModel;

import src.domain.PostOffice;

import src.resource.DialogResource;
import src.resource.ValidateResource;
import src.resource.DialogResource.windowTypeEnum;
import src.service.ClientService;
import src.view.ManagePostOffices;


public class ManagePostOfficesController {
	
	private ClientService system;
	private ManagePostOffices view;
	

	public ManagePostOfficesController(ClientService system) {
		this.view = ManagePostOffices.getInstance(this);
		this.system = system;
	}

	
	public void  performUpdatePostOffice() throws RemoteException{
		if(this.view.getjListPostOffice().getSelectedValue()==null){
			DialogResource.showMessage(view.getBtnSave().getLocationOnScreen(),"Information","Must select a Post office.",windowTypeEnum.wtInformation);
			return;
		}
		
		if(!validateRequiredFields()){
			DialogResource.showMessage(view.getBtnSave().getLocationOnScreen(),"Information","Must complete the required fields.",windowTypeEnum.wtInformation);
			return;
		}
		if(!validateFieldsLenght()){
			DialogResource.showMessage(view.getBtnSave().getLocationOnScreen(),"Information","The fields can not contain more than 50 characters.",windowTypeEnum.wtInformation);
			return;
		}
		
		/*if(system.getPostOfficeByName(view.getTxtName().getText())!=null){
			DialogResource.showMessage(view.getBtnSave().getLocationOnScreen(),"Information","Name in use. Please enter another name.",windowTypeEnum.wtInformation);
			return;
		}*/
		
		((PostOffice)this.view.getjListPostOffice().getSelectedValue()).setName(view.getTxtName().getText());
		system.updatePostOffice((PostOffice) view.getjListPostOffice().getSelectedValue());
		DialogResource.showMessage(view.getBtnSave().getLocation(),"Information","Post Office updated sucessfully.",windowTypeEnum.wtConfirmation);
		this.cleanForm();
		this.view.getTxtSearch().setText("");
		((DefaultListModel)this.view.getJListModel()).removeAllElements();
	}
	
	private void cleanForm(){
		view.getTxtName().setText("");
		
	}
	
	private boolean validateRequiredFields(){
		return ValidateResource.validateRequiredField(view.getTxtName().getText());
	}
	
	private boolean validateFieldsLenght(){
		return ValidateResource.validateStringLength(view.getTxtName().getText(), 50); 
	}
	
	public void performDeletePostOffice() throws RemoteException{
		if(view.getjListPostOffice().getSelectedValue()==null){
			DialogResource.showMessage(view.getBtnDelete().getLocationOnScreen(),"Information","Must select a user.",windowTypeEnum.wtInformation);
			return;
		}
		
		if(system.getMailBoxesByPostOffice(((PostOffice)this.view.getjListPostOffice().getSelectedValue()).getId()).size()!=0){
			DialogResource.showMessage(view.getBtnDelete().getLocationOnScreen(),"Information","You can not delete. This PostOffice has mailboxes associated.",windowTypeEnum.wtInformation);
			return;
		}
		system.deletePostOffice(((PostOffice)view.getjListPostOffice().getSelectedValue()).getId());
		((DefaultListModel)this.view.getJListModel()).removeElement((PostOffice)view.getjListPostOffice().getSelectedValue());
		this.cleanForm();
		this.view.getTxtSearch().setText("");
		
		DialogResource.showMessage(view.getBtnDelete().getLocationOnScreen(),"Information","User deleted sucessfully.",windowTypeEnum.wtConfirmation);
	}
	
	public void performSelectItem() throws RemoteException{
		if(view.getjListPostOffice().getSelectedValue()==null)
			return;
		this.bindPostOffice();
		
	}
	
	private void bindPostOffice(){
		view.getTxtName().setText(((PostOffice)view.getjListPostOffice().getSelectedValue()).getName());
	}
	
	public void performSearchPostOffices() throws RemoteException{
		this.cleanForm();
		((DefaultListModel)this.view.getJListModel()).removeAllElements();
		view.setJListModel(toListModel(system.getPostOffices(view.getTxtSearch().getText())));
		view.getjListPostOffice().setModel(view.getJListModel());
		
	}
	
	private ListModel toListModel(Set<PostOffice> set){
		ListModel l=new DefaultListModel();
		for(PostOffice p:set)
			((DefaultListModel) l).addElement(p);
		return l;
	}
	
	


}
